package wechat.base.snd;

import org.apache.log4j.Logger;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.json.JSONObject;
import wechat.base.snd.Department;
import wechat.base.snd.util.ExecPost;

@SuppressWarnings("all")
public class DepartmentManager {
  private static String WECHAT_DEPARTMENT_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=";
  
  private static Logger logger = Logger.getLogger("DepartmentManager");
  
  public static Object getDepartmentManager() {
    return DepartmentManager.getDepartmentManager();
  }
  
  @Accessors
  private String token;
  
  public DepartmentManager(final String token) {
    this.token = token;
  }
  
  public String getDepartmentUrl() {
    return (DepartmentManager.WECHAT_DEPARTMENT_URL + this.token);
  }
  
  private boolean ret = true;
  
  public boolean addDepartment(final Department d) {
    boolean _xblockexpression = false;
    {
      String _departmentUrl = this.getDepartmentUrl();
      String _plus = ("url is " + _departmentUrl);
      DepartmentManager.logger.info(_plus);
      JSONObject _jsonBuilder = d.jsonBuilder();
      String _string = _jsonBuilder.toString();
      String _plus_1 = ("json string is " + _string);
      DepartmentManager.logger.info(_plus_1);
      String _departmentUrl_1 = this.getDepartmentUrl();
      JSONObject _jsonBuilder_1 = d.jsonBuilder();
      String _string_1 = _jsonBuilder_1.toString();
      String _excuteJsonPost = ExecPost.excuteJsonPost(_departmentUrl_1, _string_1);
      final Procedure1<String> _function = new Procedure1<String>() {
        public void apply(final String it) {
          try {
            JSONObject _jSONObject = new JSONObject(it);
            final Procedure1<JSONObject> _function = new Procedure1<JSONObject>() {
              public void apply(final JSONObject it) {
                try {
                  String _string = it.getString("errcode");
                  String _plus = ("Add department return code is " + _string);
                  DepartmentManager.logger.info(_plus);
                  String _string_1 = it.getString("errcode");
                  boolean _equals = _string_1.equals("0");
                  DepartmentManager.this.ret = _equals;
                  String _string_2 = it.getString("id");
                  d.setId(_string_2);
                } catch (Throwable _e) {
                  throw Exceptions.sneakyThrow(_e);
                }
              }
            };
            ObjectExtensions.<JSONObject>operator_doubleArrow(_jSONObject, _function);
          } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
          }
        }
      };
      ObjectExtensions.<String>operator_doubleArrow(_excuteJsonPost, _function);
      _xblockexpression = this.ret;
    }
    return _xblockexpression;
  }
  
  @Pure
  public String getToken() {
    return this.token;
  }
  
  public void setToken(final String token) {
    this.token = token;
  }
}
